import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/HttpServletRequest")
public class HttpServletRequest extends HttpServlet {
    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(req.getMethod());
        stringBuilder.append("</br>");
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("</br>");
        stringBuilder.append(req.getContextPath());
        stringBuilder.append("</br>");
        stringBuilder.append(req.getQueryString());
        stringBuilder.append("</br>");
        String id = req.getParameter("id");
        stringBuilder.append("id==" + id);
        String name = req.getParameter("name");
        stringBuilder.append(" name==" + name);
        stringBuilder.append("</br>");
        stringBuilder.append(req.getContentType());
        stringBuilder.append("</br>");
        Enumeration<String> enumeration = req.getHeaderNames();
        while (enumeration.hasMoreElements()){
            String key = enumeration.nextElement();
            stringBuilder.append(key + ": ");
            stringBuilder.append(req.getHeader(key));
            stringBuilder.append("</br>");
        }

        resp.getWriter().write(stringBuilder.toString());
    }

    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        StringBuilder stringBuilder = new StringBuilder();
        Enumeration<String> enumeration = req.getHeaderNames();
        while (enumeration.hasMoreElements()){
            String key = enumeration.nextElement();
            stringBuilder.append(key + " ");
            stringBuilder.append(req.getHeader(key));
            stringBuilder.append("</br>");
        }
        resp.getWriter().write(stringBuilder.toString());
    }
}
